package com.hejia.bulbul.controller;

import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import com.hejia.alauda.entity.BankInfo;
import com.hejia.alauda.entity.PayGate;
import com.hejia.alauda.entity.PayGateBank;
import com.hejia.alauda.service.PaymentService;
import com.hejia.alauda.utils.Pager;

@Controller
@RequestMapping("paygate")
public class PayGateController extends BaseController {

	@Resource
	private PaymentService payGateService;

	@Resource
	private PaymentService payGateBankService;

	@Resource
	private PaymentService bankInfoService;

	/**
	 * 获取支付网关列表
	 * 
	 * @param model
	 * @param pager
	 * @return
	 */
	@RequestMapping(value = "paygatelist", method = RequestMethod.GET)
	public String paygateList(ModelMap model, Pager<PayGate> pager) {
		Pager<PayGate> payGate = payGateService.findAllPayGate(pager);
		model.put("payGate", payGate);
		return "/setting/gateway_list";
	}

	/**
	 * 跳转到添加界面
	 */
	@RequestMapping(value = "paygateadd", method = RequestMethod.GET)
	public String addPayGate(ModelMap model) {
		List<BankInfo> banklist = bankInfoService.findAllBankInfo();
		model.put("banklist", banklist);
		return "/setting/gateway_input";
	}

	/**
	 * 添加完保存
	 * 
	 * @param payGate
	 * @param bankIds
	 * @param model
	 * @param redirectAttributes
	 * @return
	 */
	@RequestMapping(value = "paygateadd", method = RequestMethod.POST)
	public String savePayGate(PayGate payGate,
			@RequestParam(value = "bankIds") Long[] bankIds, ModelMap model,
			RedirectAttributes redirectAttributes) {

		payGate.setCreateDate(new Date());
		if (payGateService.savePayGate(payGate)) {

			for (Long id : bankIds) {
				if (id == null) {
					continue;
				}
				PayGateBank payGateBank = this.payGateBankService
						.findPayGateBankById(id);
				payGateBank.setPaygateid(payGate.getId());
				payGateBank.setModifyDate(new Date());
				payGateBankService.modifyPayGateBank(payGateBank);
			}

			redirectAttributes.addFlashAttribute("status", STATUS_SUCCESS);
			redirectAttributes.addFlashAttribute("msg", "支付网关添加成功");
		} else {
			redirectAttributes.addFlashAttribute("status", STATUS_FAILD);
			redirectAttributes.addFlashAttribute("msg", "支付网关添加失败");
		}
		return "redirect:/paygate/paygatelist";
	}

	/**
	 * 跳转到修改界面
	 */
	@RequestMapping(value = "paygateupdate/{id}", method = RequestMethod.GET)
	public String updatePayGate(@PathVariable Long id, ModelMap model) {
		PayGate payGate = payGateService.getPayGateById(id);
		List<PayGateBank> pblist = payGateBankService
				.getPayGateBankByPayGateId(id);
		List<BankInfo> banklist = bankInfoService.findAllBankInfo();
		model.put("banklist", banklist);
		model.put("payGate", payGate);
		model.put("pblist", pblist);
		return "/setting/gateway_input";
	}

	/**
	 * 修改完保存
	 */
	@RequestMapping(value = "paygateupdate/{id}", method = RequestMethod.POST)
	public String saveUpdatePayGate(@PathVariable Long id, PayGate payGate,
			@RequestParam(value = "bankIds") Long[] bankIds,
			RedirectAttributes redirectAttributes, ModelMap model) {
		PayGate payGate2 = payGateService.getPayGateById(id);
		payGate2.setGateName(payGate.getGateName());
		payGate2.setGateCode(payGate.getGateCode());
		payGate2.setModifyDate(new Date());
		payGate2.setPriority(payGate.getPriority());
		payGate2.setInuse(payGate.getInuse());

		if (payGateService.modifyPayGate(payGate2)) {
			for (Long pid : bankIds) {
				if (pid == null) {
					continue;
				}
				PayGateBank payGateBank = this.payGateBankService
						.findPayGateBankById(pid);
				payGateBank.setPaygateid(payGate.getId());
				payGateBank.setModifyDate(new Date());
				payGateBankService.modifyPayGateBank(payGateBank);
			}
			redirectAttributes.addFlashAttribute("status", STATUS_SUCCESS);
			redirectAttributes.addFlashAttribute("msg", "支付网关修改成功");
		} else {
			redirectAttributes.addFlashAttribute("status", STATUS_FAILD);
			redirectAttributes.addFlashAttribute("msg", "支付网关修改失败");
		}

		return "redirect:/paygate/paygatelist";
	}

	/**
	 * 删除
	 */
	@RequestMapping(value = "paygatedelete/{id}", method = RequestMethod.GET)
	public String deletePayGate(@PathVariable Long id,
			RedirectAttributes redirectAttributes) {
		if (payGateService.deletePayGate(id)) {
			payGateBankService.deletePayGateBankByPid(id);
			redirectAttributes.addFlashAttribute("status", STATUS_SUCCESS);
			redirectAttributes.addFlashAttribute("msg", "支付网关删除成功");
		} else {
			redirectAttributes.addFlashAttribute("status", STATUS_FAILD);
			redirectAttributes.addFlashAttribute("msg", "支付网关删除失败");
		}

		return "redirect:/paygate/paygatelist";
	}

}
